package com.bdth.bdthiss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.bdth.bdthiss.entity.Contract;
import com.bdth.bdthiss.mapper.ContractMapper;
import com.bdth.bdthiss.service.ContractService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthiss.vo.ContractPlusVo;
import com.bdth.bdthiss.vo.Pagination;
import com.bdth.bdthiss.vo.SysMoney;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 合同信息 服务实现类
 * </p>
 *
 * @author LGP
 * @since 2019-08-26
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Override
    public Page<Contract> listPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        String name = (String)query.get("name");
        if(name!=null && !name.trim().equals(""))
            entityWrapper.like("name",name);
        String code = (String)query.get("code");
        if(code!=null && !code.trim().equals(""))
            entityWrapper.like("code",code);
        String type = (String)query.get("type");
        if(type!=null && !type.trim().equals(""))
            entityWrapper.eq("type",type);
        String status = (String)query.get("status");
        if(status!=null && !status.trim().equals(""))
            entityWrapper.eq("status",status);
        String total = (String)query.get("total");
        if(total!=null && !total.trim().equals(""))
            entityWrapper.eq("total",Double.valueOf(total));
        String crtime = (String)query.get("crtime");
        if(crtime!=null && !crtime.trim().equals("")) {
            entityWrapper.ge("crtime", crtime+"-01 00:00:01");
            entityWrapper.le("crtime", crtime+"-31 23:59:59");
        }
        try {
            Integer signmanid = (Integer) query.get("signmanid");
            if (signmanid != null)
                entityWrapper.in("signmanid", signmanid+"");
        }catch (Exception ex){
            String signmanid = (String) query.get("signmanid");
            if (signmanid != null)
                entityWrapper.in("signmanid", signmanid);
        }
        String introtype = (String)query.get("introtype");
        if(introtype!=null && !introtype.trim().equals(""))
            entityWrapper.eq("introtype",introtype);
        String contractIds = (String)query.get("contractIds");
        if(contractIds!=null && !contractIds.trim().equals(""))
            entityWrapper.in("id",contractIds);
        if(query.get("del")!=null ) {
            Integer del = Integer.parseInt((String) query.get("del"));
            entityWrapper.eq("del", del);
        }
//        entityWrapper.orderBy("code",false);
//        entityWrapper.orderBy("id",false);
//        if(pagination.getOrder()!=null) {
//            if (pagination.getOrder().toLowerCase().equals("asc")){
//                entityWrapper.orderBy(pagination.getSort(), true);
//            }else{
//                entityWrapper.orderBy(pagination.getSort(), false);
//            }
//        }
        entityWrapper.last("order by cast(substring(code,7) as unsigned) desc");
        Page<Contract> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<Contract> items = contractMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }

    @Override
    public boolean update(Contract bean) {
        if(bean.getId()>0) {
            Integer result = contractMapper.updateById(bean);
            if(result>0)
                return true;
        }
        return false;
    }

    @Override
    public Contract save(Contract bean) {
        Integer result = contractMapper.insertAndGetId(bean);
        if(result>0)
            return bean;
        return null;
    }

    @Override
    public boolean delete(Integer id) {
        Contract bean = new Contract();
        bean.setId(id);
        bean.setDel(1);
        Integer result = contractMapper.updateById(bean);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean remove(String ids) {
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        Contract bean = new Contract();
        bean.setDel(1);
        if(ids!=null || ids.equals(""))
            return false;
        entityWrapper.in("id",ids);
        //int result = contractMapper.delete(entityWrapper);
        int result = contractMapper.update(bean,entityWrapper);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean filish(Integer ids) {
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        Contract bean  = new Contract();
        bean.setId(ids);
        bean.setStatus("合同完成");
        Integer result = contractMapper.updateById(bean);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public Contract selectByCode(String code) {
        EntityWrapper<Contract> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("code",code);
        List<Contract> list = contractMapper.selectList(entityWrapper);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public SysMoney getMoney() {
        return contractMapper.getMoney();
    }

    @Override
    public Page<ContractPlusVo> listPlusPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<ContractPlusVo> entityWrapper = new EntityWrapper<>();

        Page<ContractPlusVo> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());
        PageHelper.startPage(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());
        List<ContractPlusVo> items = contractMapper.getPlusList(null);
        page.setRecords(items);
        return page;
    }
}
